package com.wswy.wyjk.ui.practice

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.raft.framework.base.RFBaseFragment
import com.raft.framework.ui.adapter.SampleRecyclerViewAdapter
import com.wswy.wyjk.AppContext
import com.wswy.wyjk.R
import com.wswy.wyjk.db.DataLoader
import com.wswy.wyjk.model.Chapter
import com.wswy.wyjk.model.PracticeType
import com.wswy.wyjk.ui.EXTRA_DATA
import com.wswy.wyjk.ui.common.BaseListFragment
import com.wswy.wyjk.ui.common.Router
import com.wswy.wyjk.ui.practiceNew.PracticeNewActivity
import com.wswy.wyjk.utils.loadLayout
import timber.log.Timber

/**
 * 章节列表
 */
class ChapterListFragment : BaseListFragment() {
    companion object {

        fun newInstance(): RFBaseFragment {
            return ChapterListFragment()
        }

    }

    override fun getPageTitle(): String? {
        return AppContext.getContext().getString(R.string.chapter_list_page_title)
    }

    override fun onInit(recyclerView: RecyclerView) {

        arguments?.getParcelable<PracticeType?>(EXTRA_DATA)?.let { practiceType ->

            DataLoader(practiceType).getChapters()?.let {

                recyclerView.adapter = ChapterListAdapter().apply {

                    setOnItemClickListener { view, _, t ->

                        practiceType.copy().apply {
                            type = PracticeType.TYPE_CHAPTER
                            chapterId = t?.mid ?: 0
                        }.also {
                            //章节练习
                            //.start(view.context, Router.PRACTICE,practiceType = it)
                            context!!.startActivity(
                                Intent(context, PracticeNewActivity::class.java).putExtra("type", 4)
                                    .putExtra("chapterId", it.chapterId)
                            )
                        }
                    }

                    setNewData(it)
                    notifyDataSetChanged()
                }


            }


        }

    }


    class ChapterListAdapter :
        SampleRecyclerViewAdapter<Chapter, ChapterListAdapter.ChapterViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChapterViewHolder {
            return ChapterViewHolder(loadLayout(parent, R.layout.layout_chapter_item))
        }

        override fun onBindViewHolder(holder: ChapterViewHolder, position: Int, data: Chapter?) {
            data?.run {

                holder.tvPosition.text = "${position + 1}"

                holder.tvChapterName.text = title
                holder.tvCount.text = "${count}题"

            }
        }


        class ChapterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            val tvPosition: TextView = itemView.findViewById(R.id.tv_position)
            val tvChapterName: TextView = itemView.findViewById(R.id.tv_chapter_title)
            val tvCount: TextView = itemView.findViewById(R.id.tv_chapter_count)

        }
    }

}